package com.yinhe.music.yhmusic.localmusic.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.common.utils.ScreenUtils;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.ViewPagerFragment;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.downmusic.DownloadTask;
import com.yinhe.music.yhmusic.localmusic.adapter.LocalAlbumAdapter;
import com.yinhe.music.yhmusic.model.AlbumInfo;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.utils.MusicUtils;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by hasee on 2017/8/14.
 */

public class LocalAlbumbFragment extends ViewPagerFragment implements BaseQuickAdapter.OnItemClickListener {

    public static final String TAG = "LocalMusicFragment";

    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView mRecyclerview;

    Unbinder unbinder;
    private List<Music> musicList = new ArrayList<>();
    private List<AlbumInfo> albumInfoList = new ArrayList<>();
    private LocalAlbumAdapter mAdapter;

    public static LocalAlbumbFragment newInstance() {
        Bundle args = new Bundle();
        LocalAlbumbFragment fragment = new LocalAlbumbFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recycler_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        RxBus.get().register(this);
        musicList = CacheManager.getInstance().getScanMusic();
        getAlbum();

        mAdapter = new LocalAlbumAdapter(albumInfoList,musicList);
        mAdapter.setEmptyView(inflater.inflate(R.layout.include_empty_layout, (ViewGroup) mRecyclerview.getParent(), false));
        mAdapter.setOnItemClickListener(this);
        mRecyclerview.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerview.setHasFixedSize(true);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, ScreenUtils.dp2px(20), false);
        mRecyclerview.addItemDecoration(itemDecoration);
        mRecyclerview.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        RxBus.get().unregister(this);
        super.onDestroyView();
        unbinder.unbind();
    }

    private void getAlbum(){
        if (musicList != null && !musicList.isEmpty()){
            for (Music music : musicList){
                AlbumInfo albumInfo = new AlbumInfo();
                albumInfo.setAlbumId((int) music.getAlbumId());
                albumInfo.setImage(music.getImage());
                albumInfo.setAlbumName(music.getAlbumName());
                if (!albumInfoList.contains(albumInfo)){
                    albumInfoList.add(albumInfo);
                }
            }
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AlbumInfo albumInfo = (AlbumInfo) adapter.getData().get(position);
        List<Music> mList = MusicUtils.getAlbumSong(musicList,albumInfo.getAlbumId());
        CacheManager.getInstance().setIntentList(mList);
        Intent intent = new Intent(getActivity(),LocalSingerActivity.class);
        intent.putExtra("singerName",albumInfo.getAlbumName());
        startActivity(intent);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Local.DELETE_LOCAL_MUSIC)
    )
    public void onRxbusLocalMusic(RxbusListObject rxbusListObject) {
        List<Music> deleteList = (List<Music>) rxbusListObject.rxList;
        if (deleteList != null) {
            musicList.removeAll(deleteList);
            getAlbum();
            mAdapter.notifyDataSetChanged();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Download.DOWNLOAD_COMPLETE)
    )
    public void onRxbusDownloadComplete(DownloadTask downloadTask) {
        musicList = CacheManager.getInstance().getScanMusic();
        getAlbum();
        mAdapter.notifyDataSetChanged();
    }
}
